{% extends "admin/base_site_nav.html" %}
{% load rest_framework %}
{% load i18n %}

{% block extrahead %}{{block.super}}
<script>
$(function() {
  $("#jwt").on('input', function() {
  	window.location.href = '{{request.prefix}}/api/?exp=' + $("#jwt").val();
  });
});
</script>
{% endblock %}

{% block content %}
<div id="content-summary" class="row"><div class="col-md-12">
{% blocktrans with username=request.user.username %}This page helps developers to learn and experiment with the
<a style="text-decoration:underline" target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a>
API of frePPLe.<br>
This API allows your application to exchange information with frePPLe.<br>
<br>
The "list API" link takes you to the object list page.<br>
And the "detail API" link takes you to a specific object when you enter the object's primary key.<br>
In these pages you will be able to perform HTTP requests with methods GET, POST, OPTIONS, PUT, PATCH and DELETE.<br>
The results can be shown in JSON format or as HTML in your browser.<br>
<br>
Using tools like 'wget' or 'curl' you can access the API from the command line.<br>
For instance, to return the list of all sales orders in JSON format:<br><br>
<div style="margin-left:20px">
Using <a style="text-decoration:underline" href="https://en.wikipedia.org/wiki/Basic_access_authentication" target="_blank">basic authentication</a>:<br>
<div class="well well-sm" style="margin-top:5px">
wget --http-user={{username}} --http-password=PASSWORD http://127.0.0.1:8000/api/input/demand/<br><br>
curl -u {{username}}:PASSWORD http://127.0.0.1:8000/api/input/demand/
</div>
Using <a style="text-decoration:underline" href="https://jwt.io/" target="_blank">JSON Web Token authentication</a> valid for the next <input id="jwt" type="number" value="{{exp}}" min="0" step="1" style="width:4em"/>  days:<br>
<div class="well well-sm" style="margin-top:5px">
wget --header 'Authorization: Bearer {{token}}' http://127.0.0.1:8000/api/input/demand/<br><br>
curl -H 'Accept: application/json' --header 'Authorization: Bearer {{token}}' http://127.0.0.1:8000/api/input/demand/
</div>
</div>
{% endblocktrans %}
</div></div>

<div class="row"><div class="col-md-12" style="max-width:1100px">
  <div class="panel panel-default">
    <div class="panel-heading">
      <b>REST API</b>
    </div>
    <table class="table table-condensed table-hover">
      <tr><th></th><th>{% trans 'List API' %}</th><th>{% trans 'Detail API' %}</th></tr>
      {% getMenu as menu %}
      {% for group in menu %}
        {% for item in group.1 %}
          {% if item.1.model and not item.1.excludeFromBulkOperations %}
          <tr>
            <td> {{ group.0 }} - {{ item.0 }} </td>
            <td>
              <a href="{{request.prefix}}/api/{{  item.1.model|app_label }}/{{  item.1.model|short_model_name }}/ ">
                {{request.prefix}}/api/{{  item.1.model|app_label }}/{{  item.1.model|short_model_name }}/
                <span class="leftpadding fa fa-caret-right" role="input/item"></span>
              </a>
            </td>
            <td><span style="cursor: pointer">
              {{request.prefix}}/api/{{  item.1.model|app_label }}/{{  item.1.model|short_model_name }}/
              <input type="text" class="vTextField" onChange ="window.location.href = '{{request.prefix}}/api/{{ item.1.model|app_label }}/{{  item.1.model|short_model_name }}/'+encodeURI($(event.target).val())+'/'" maxlength="30">
              </input>/<span class="leftpadding fa fa-caret-right"></span>
            </span></td>
          </tr>
          {% endif %}
        {% endfor %}
      {% endfor %}
    </table>
  </div>
</div></div>

{% endblock %}
